#include "motor.h"

void Motor_Init(Motor *motor, GPTIMER_Regs *timer_pwm, uint32_t channel,
                GPIO_Regs*port1, uint32_t pin1, GPIO_Regs*port2, uint32_t pin2)
{
    motor->timer_pwm = timer_pwm;
    motor->channel = channel;
    motor->port1 = port1;
    motor->pin1 = pin1;
    motor->port2 = port2;
    motor->pin2 = pin2;
}

void Servo_Init(Servo *Servo, GPTIMER_Regs *timer_pwm, uint32_t channel)
{
  Servo->timer_pwm=timer_pwm;
  Servo->channel=channel;
}


void Motor_Driver(Motor *motor, int32_t speed_set, int if_front)//left为1，right为
{
    if (if_front == 1) {
    // 正转
    DL_GPIO_setPins(motor->port1, motor->pin1);
    DL_GPIO_clearPins(motor->port2, motor->pin2);
  } else if (if_front == 0) {
    // 反转
   DL_GPIO_clearPins(motor->port1, motor->pin1);
   DL_GPIO_setPins(motor->port2, motor->pin2);
  } else if(speed_set == 0) {
    DL_GPIO_clearPins(motor->port1, motor->pin1);
    DL_GPIO_clearPins(motor->port2, motor->pin2);
  }

  DL_TimerA_setCaptureCompareValue(motor->timer_pwm, speed_set, motor->channel);
  


}

void Servo_Driver(Servo*steer,int32_t angle_set)
{
  steer->angle_set=angle_set;
  DL_TimerA_setCaptureCompareValue(steer->timer_pwm, steer->angle_set, steer->channel);
}

